package com.example.demo.controller;

import com.example.demo.pojo.Result;
import com.example.demo.pojo.entity.Subject;
import com.example.demo.pojo.vo.SubjectVO;
import com.example.demo.service.imply.ISubjectServiceImply;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.io.*;
/**
 * @author fjh
 */
@RestController
@RequestMapping("/subject")
@Slf4j
@Api(tags = "学科相关接口")
@RequiredArgsConstructor
@CrossOrigin(origins = {"http://localhost:8080"}, allowCredentials = "true")
public class SubjectController {
    private final ISubjectServiceImply subjectService;
    private static final String FILE_DIRECTORY = "C:\\Users\\LEGION\\Desktop\\1.txt";
    @ApiOperation("初始化学科")
    @PostMapping("/init")
    public Result initBank(){
        List<SubjectVO>subjectList=subjectService.initSubject();
        if(subjectList!=null){
            return Result.success(subjectList);
        }else{
            return Result.error("学科初始化失败");
        }
    }
    @ApiOperation("根据id查找学科")
    @PostMapping("/searchById")
    public Result searchSubject(int id){
        System.out.println("查找的id是"+id);
        Subject subject=subjectService.searchById(id);
        if(subject!=null){
            return Result.success(subject);
        }else{
            return Result.error("学科不存在");
        }
    }
    @ApiOperation("添加课程")
    @PostMapping("/addSubject")
    public Result addSubject(Subject subject){
        System.out.println("传入的参数");
        System.out.println(subject);
        int result=subjectService.addSubject(subject);
        if(result==0){
            return Result.error("失败");
        }
        else{
            return Result.success();
        }
    }
    @ApiOperation("上传文件")
    @PostMapping("/upload")
    public Result uploadFile(MultipartFile file,int subjectId,int fileId) throws  Exception{
        if (file == null || file.isEmpty()) {
            return Result.error("上传的文件为空");
        }
        List<String> fileCategory = Arrays.asList("txt", "csv", "xls");
        String fileName=file.getOriginalFilename();
        int i=fileName.lastIndexOf('.');
        if(i>0){
            if(fileCategory.contains(fileName.substring(i+1))){
                subjectService.uploadFile(file,subjectId,fileId);
            }
        }
        return Result.success();
    }
@ApiOperation("123")
@GetMapping("/download")
@ResponseBody
public ResponseEntity<Resource> downloadFile() {
    Path filePath = Paths.get(FILE_DIRECTORY);
    try {
        Resource resource = new org.springframework.core.io.UrlResource(filePath.toUri());
        if (resource.exists() || resource.isReadable()) {
            return ResponseEntity.ok()
                    .contentType(MediaType.APPLICATION_OCTET_STREAM)
                    .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
                    .body(resource);
        } else {
            return ResponseEntity.notFound().build();
        }
    } catch (IOException ex) {
        ex.printStackTrace();
        return ResponseEntity.status(500).build();
    }
}
}
